//////#include<algorithm>
//////#include<iostream>
//////#include<string>
//////const int N = 1e6 + 10;
//////int a[N];
//////using namespace std;
//////int main()
//////{
//////	int T;cin >> T;
//////	while (T--)
//////	{
//////		string s1;cin >> s1;
//////		int p;cin >> p;
//////		string s2 = s1;
//////		sort(s2.begin(), s2.end());
//////		string s3 = "",s4 = s1;
//////		int sum = 0;
//////		int la = s1.size(), lb = s2.size();
//////		reverse(s4.begin(), s4.end());
//////		if (s1 == s4)
//////		{
//////			int i = 0;
//////			while (sum < p && i < la)
//////			{
//////				sum += s1[i] - 'a' + 1;
//////				cout << s1[i];
//////				i++;
//////			}
//////			cout << endl;
//////			continue;
//////		}
//////		for (int i = 0; i < lb; i++)
//////		{
//////			sum += s2[i] - 'a' + 1;
//////			if (sum > p)
//////			{
//////				s3 += s2[i];
//////			}
//////		}
//////		for (int i = 0; i < la; i++)
//////		{
//////			if (s3.find(s1[i]) > 2e9 + 10 or (s3.find(s1[i]) && s1.find(s3[s1[i]])))
//////			{
//////				cout << s1[i];
//////			}
//////		}
//////		cout << endl;
//////	}
//////	return 0;
//////}
////#include<iostream>
////#include<string>
////using namespace std;
////int main()
////{
////	string s1;cin >> s1;
////	for (int i = 1; i <= s1.size();i++)
////	{
////		if (i % 2 == 0) s1.erase(s1.begin() + i - 1);
////	}
////	cout << s1;
////	return 0;
////}
#include<iostream>
#include<algorithm>
#include<string>
using namespace std;
int st[28];
int main()
{
	int T;cin >> T;
	while (T--)
	{
		string s1;cin >> s1;
		int p;cin >> p;
		string s2 = s1;
		int sum = 0;
		sort(s2.begin(), s2.end());
		string s3 = "";int i = 0;
		auto la = s1.size();
		while (sum <= p && i < la)
		{
			sum += s2[i] - 'a' + 1;
			if (sum > p) break;
			s3 += s2[i];
			st[s2[i] - 'a' + 1]++;
			i++;
		}
		for (auto i = 0; i < la; i++)
		{
			auto v = s1[i];
			if (s3.find(v) >= 0 && s3.find(v) <= 1e7 + 10 && st[v - 'a' + 1])
			{
				cout << v;
				st[v -'a' + 1]--;
			}
		}
		cout << endl;
	}
	return 0;
}
//#include<iostream>
//#include<string>
//using namespace std;
//int main()
//{
//	string s1 = "abcd";
//	string s2 = "ac";
//	cout << s2.find(s1[2]) << endl;
//	return 0;
//}